<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Creation of Dates - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.date.creation.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.date.creation.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.date.overview.html">Zend_Date API Overview</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.date.html">Zend_Date</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.date.constants.html">Constants for General Date Functions</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.date.creation" class="section"><div class="info"><h1 class="title">Creation of Dates</h1></div>
    

    <p class="para">
        <span class="classname">Zend_Date</span> provides several different ways to create a new instance of
        itself. As there are different needs the most convenient ways will be shown in this chapter.
    </p>

    <div class="section" id="zend.date.creation.actual"><div class="info"><h1 class="title">Create the Actual Date</h1></div>
        

        <p class="para">
            The simplest way of creating a date object is to create the actual date. You can either
            create a new instance with <strong class="command">new Zend_Date()</strong> or use the convenient
            static method  <span class="methodname">Zend_Date::now()</span> which both will return the
            actual date as new instance of <span class="classname">Zend_Date</span>. The actual date always
            include the actual date and time for the actual set timezone.
        </p>

        <div class="example" id="zend.date.creation.actual.example-1"><div class="info"><p><b>Example #1 Date Creation by Instance</b></p></div>
            

            <div class="example-contents"><p>
                Date creation by creating a new instance means that you do not need to give an
                parameter. Of course there are several parameters which will be described later but
                normally this is the simplest and most used way to get the actual date as
                <span class="classname">Zend_Date</span> instance.
            </p></div>

            <pre class="programlisting brush: php">
$date = new Zend_Date();
</pre>

        </div>

        <div class="example" id="zend.date.creation.actual.example-2"><div class="info"><p><b>Example #2 Static Date Creation</b></p></div>
            

            <div class="example-contents"><p>
                Sometimes it is easier to use a static method for date creation. Therefor you can
                use the <em class="emphasis"> <span class="methodname">now()</span></em> method. It returns a
                new instance of <span class="classname">Zend_Date</span> the same way as if you would use
                <strong class="command">new Zend_Date()</strong>. But it will always return the actual date and
                can not be changed by giving optional parameters.
            </p></div>

            <pre class="programlisting brush: php">
$date = Zend_Date::now();
</pre>

        </div>
    </div>

    <div class="section" id="zend.date.creation.database"><div class="info"><h1 class="title">Create a Date from Database</h1></div>
        

        <p class="para">
            Databases are often used to store date values. But the problem is, that every database
            outputs its date values in a different way. <em class="emphasis">MsSQL</em> databases use a
            quite different standard date output than <em class="emphasis">MySQL</em> databases. But for
            simplification <span class="classname">Zend_Date</span> makes it very easy to create a date
            from database date values.
        </p>

        <p class="para">
            Of course each database can be said to convert the output of a defined column to a
            special value. For example you could convert a <em class="emphasis">datetime</em> value to
            output a minute value. But this is time expensive and often you are in need of handling
            dates in an other way than expected when creating the database query.
        </p>

        <p class="para">
            So we have one quick and one convenient way of creating dates from database values.
        </p>

        <div class="example" id="zend.date.creation.database.example-1"><div class="info"><p><b>Example #3 Quick Creation of Dates from Database Date Values</b></p></div>
            

            <div class="example-contents"><p>
                All databases are known to handle queries as fast as possible. They are built to act
                and respond quick. The quickest way for handling dates is to get unix timestamps
                from the database. All databases store date values internal as timestamp (not unix
                timestamp). This means that the time for creating a timestamp through a query is
                much smaller than converting it to a specified format.
            </p></div>

            <pre class="programlisting brush: php">
// SELECT UNIX_TIMESTAMP(my_datetime_column) FROM my_table
$date = new Zend_Date($unixtimestamp, Zend_Date::TIMESTAMP);
</pre>

        </div>

        <div class="example" id="zend.date.creation.database.example-2"><div class="info"><p><b>Example #4 Convenient Creation of Dates from Database Date Values</b></p></div>
            

            <div class="example-contents"><p>
                The standard output of all databases is quite different even if it looks the same on
                the first eyecatch. But all are part of the <acronym class="acronym">ISO</acronym> Standard and
                explained through it. So the easiest way of date creation is the usage of
                <b><tt>Zend_Date::ISO_8601</tt></b>. Databases which are known to be
                recognised by <b><tt>Zend_Date::ISO_8601</tt></b> are
                <em class="emphasis">MySQL</em>, <em class="emphasis">MsSQL</em> for example. But all
                databases are also able to return a <acronym class="acronym">ISO-8601</acronym> representation of a
                date column. <acronym class="acronym">ISO-8601</acronym> has the big advantage that it is human
                readable. The disadvantage is that <acronym class="acronym">ISO-8601</acronym> needs more time for
                computation than a simple unix timestamp. But it should also be mentioned that unix
                timestamps are only supported for dates after 1 January 1970.
            </p></div>

            <pre class="programlisting brush: php">
// SELECT datecolumn FROM my_table
$date = new Zend_Date($datecolumn, Zend_Date::ISO_8601);
</pre>

        </div>
    </div>

    <div class="section" id="zend.date.creation.array"><div class="info"><h1 class="title">Create Dates from an Array</h1></div>
        

        <p class="para">
            Dates can also be created by the usage of an array. This is a simple and easy way. The
            used array keys are:
        </p>

        <ul class="itemizedlist">
            <li class="listitem"><p class="para"><em class="emphasis">day</em>: day of the date as number</p></li>

            <li class="listitem">
                <p class="para"><em class="emphasis">month</em>: month of the date as number</p>
            </li>

            <li class="listitem"><p class="para"><em class="emphasis">year</em>: full year of the date</p></li>
            <li class="listitem"><p class="para"><em class="emphasis">hour</em>: hour of the date</p></li>
            <li class="listitem"><p class="para"><em class="emphasis">minute</em>: minute of the date</p></li>
            <li class="listitem"><p class="para"><em class="emphasis">second</em>: second of the date</p></li>
        </ul>

        <div class="example" id="zend.date.creation.array.example"><div class="info"><p><b>Example #5 Date Creation by Array</b></p></div>
            

            <div class="example-contents"><p>
                Normally you will give a complete date array for creation of a new date instance.
                But when you do not give all values, the not given array values are zeroed. This
                means that if f.e. no hour is given the hour <em class="emphasis">0</em> is used.
            </p></div>

            <pre class="programlisting brush: php">
$datearray = array(&#039;year&#039; =&gt; 2006,
                   &#039;month&#039; =&gt; 4,
                   &#039;day&#039; =&gt; 18,
                   &#039;hour&#039; =&gt; 12,
                   &#039;minute&#039; =&gt; 3,
                   &#039;second&#039; =&gt; 10);
$date = new Zend_Date($datearray);
</pre>


            <pre class="programlisting brush: php">
$datearray = array(&#039;year&#039; =&gt; 2006, &#039;month&#039; =&gt; 4, &#039;day&#039; =&gt; 18);
$date = new Zend_Date($datearray);
</pre>

        </div>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.date.overview.html">Zend_Date API Overview</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.date.html">Zend_Date</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.date.constants.html">Constants for General Date Functions</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.date.html">Zend_Date</a></li>
  <li><a href="zend.date.introduction.html">Introduction</a></li>
  <li><a href="zend.date.definition.theory.html">Theory of Operation</a></li>
  <li><a href="zend.date.basic.html">Basic Methods</a></li>
  <li><a href="zend.date.overview.html">Zend_Date API Overview</a></li>
  <li class="active"><a href="zend.date.creation.html">Creation of Dates</a></li>
  <li><a href="zend.date.constants.html">Constants for General Date Functions</a></li>
  <li><a href="zend.date.additional.html">Working Examples</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>